//
//  oldTabbarViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/15.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldTabbarViewController.h"
#import "oldNavigationViewController.h"
#import "oldNetWork.h"
#import "oldHub.h"
#import "oldMyCenterViewController.h"
#import "oldMapViewController.h"
#import "oldLoginViewController.h"
#import "AppDelegate.h"
#import "oldOrderInfo.h"
#import "oldMapViewController.h"
#import "oldFamilyFirstViewController.h"
#import "oldLocationManager.h"
#import "oldOldUserOrderListViewController.h"
#import "oldActivityFirstViewController.h"

@interface oldTabbarViewController ()<UITabBarDelegate,TokenErrorDelegate>
{
    UINavigationController *_childNavi;
}
@end

@implementation oldTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupAllChildViewControllers];
    self.tabBarController.tabBar.delegate = self;
    OLDNetWork.delegate = self;
    
//    manager = [[oldMessageManager alloc] initWithType:OrderComplete];
//    manager.delegate = self;
//    [manager registerForNotifications];
    
//    [oldLocation_SINGLETON startLocation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    
}

- ( BOOL ) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- ( BOOL ) shouldAutorotate
{
    return NO;
}

- ( UIInterfaceOrientationMask ) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)
}

- (void)setupAllChildViewControllers
{
    // 1.
    
    oldFamilyFirstViewController *home2 = [[oldFamilyFirstViewController alloc] initWithNibName:@"oldFamilyFirstViewController" bundle:nil];
    
    [self setupChildViewController:home2 title:@"发布需求" imageName:@"发布需求off" selectedImageName:@"发布需求on" index:0];
    
    oldOldUserOrderListViewController *voiceVc = [[oldOldUserOrderListViewController alloc] initWithNibName:@"oldOldUserOrderListViewController" bundle:nil];

    [self setupChildViewController:voiceVc title:@"我的订单" imageName:@"我的订单off" selectedImageName:@"我的订单on" index:1];
    
    oldActivityFirstViewController *activityVc = [[oldActivityFirstViewController alloc] initWithNibName:@"oldActivityFirstViewController" bundle:nil];
    
    [self setupChildViewController:activityVc title:@"活动" imageName:@"活动off" selectedImageName:@"活动on" index:2];
    
    oldMyCenterViewController *home3 = [[oldMyCenterViewController alloc] init];
    
    [self setupChildViewController:home3 title:@"个人中心" imageName:@"我的off" selectedImageName:@"我的on" index:3];
    
}

- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName index:(NSUInteger)index
{
    // 1.设置控制器的属性
    childVc.title = title;
    // 设置图标
    [childVc.tabBarItem setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [childVc.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName:OLD_COLOR} forState:UIControlStateSelected];
    
    // 2.包装一个导航控制器
    oldNavigationViewController *nav = [[oldNavigationViewController alloc] initWithRootViewController:childVc];
    
    _childNavi = nav;
    
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake( 0, 0, 100, 30 )];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:Title_Label_Font];
    titleLabel.text = title;
    childVc.navigationItem.titleView = titleLabel;
    [self addChildViewController:nav];
}

- (void)gotoLogout
{
    [ACCOUNT_SINGLETON clearAccountInfo];
    
    oldLoginViewController *loginVC= [[oldLoginViewController alloc] initWithNibName:@"oldLoginViewController" bundle:nil];
    MyNavigationController *navi = [[MyNavigationController alloc] initWithRootViewController:loginVC];
    UIApplication *app = [UIApplication sharedApplication];
    app.delegate.window.rootViewController = navi;
    [app.delegate.window makeKeyAndVisible];
}

-(void)gotoLoginViewController:(NSString *)errorCode{
    [oldHub warningUser:@"请重新登录"];
    [self gotoLogout];
}

- (void)gotoMapView:(oldOrderInfo *)info
{
//    [card removeSelf];
    oldMapViewController *vc = [oldMapViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.selectedViewController pushViewController:vc animated:YES];
}

- (void)swapSuppor
{
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"更换工作人员" message:@"确定要更换工作人员吗？" preferredStyle:UIAlertControllerStyleAlert];
//    
//    
//    
//    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        card.superview.hidden = NO;
//    }]];
//    
//    
//    
//    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [card changeSuppor];
//    }]];
//    
//    [self presentViewController:alertController animated:YES completion:nil];
//    
//    card.superview.hidden = YES;
}

@end
